<?php

/**
 * 默认控制器
 */
class Controller_Hostlist extends Controller_Abstract
{
    /* ******************************************************************** */
    function actionIndex()
    {
        echo 'sd';
		
		$this->_view['manage'] = Host::find('[group.groupid]= ?',$this->_context->groupid)->order('id desc')->getAll();
    }

	function actionBlank(){
		$this->_view['manage'] = Host::find('grouptohost.groupid is null')->joinLeft('grouptohost', 'groupid', 'grouptohost.hostid = hosttable.id')->getAll();
		//读取已分组主机
		//$this->_view['manage'] = Host::find('grouptohost.groupid is not null')->joinLeft('grouptohost', 'groupid', 'grouptohost.hostid = hosttable.id')->joinLeft('group', 'groupname', 'group.id = grouptohost.groupid')->getAll();
		$this->_view['form'] = new Form_BlankToGroup(url('default::hostlist/blind'));
	}

	function actionBlankform(){
		$this->_view['form'] = new Form_BlankToGroup(url('default::hostlist/blind'));
	}

	function actionGroup(){
		
		$table =QDB::getConn();
		$sql = "SELECT DISTINCT groupid FROM `grouptohost`";
		$handel =$table->execute($sql);
		$temp = $handel->fetchAll();
		$allhave = array();
		foreach ($temp as $s){
			array_push ($allhave,$s['groupid']);
			//echo $s['groupid'];
		}
		$this->_view['allgroupstr']=join(",",$allhave);
		
		//dump($this->_view['manage']);
		//		$sql = "SELECT * FROM `group` where id in (".join(",",$allhave).")";
		//		$handel =$table->execute($sql);
		//		$temp = $handel->fetchAll();
		//
		//		$this->_view['manage'] =$temp;
		//dump($temp);
	}

	function actionSystem(){
		
		$table =QDB::getConn();
		$sql = "SELECT DISTINCT groupid FROM `grouptohost`";
		$handel =$table->execute($sql);
		$temp = $handel->fetchAll();
		$allhave = array();
		foreach ($temp as $s){
			array_push ($allhave,$s['groupid']);
			//echo $s['groupid'];
		}
		$this->_view['allgroupstr']=join(",",$allhave);
		
		//dump($this->_view['manage']);
		//		$sql = "SELECT * FROM `group` where id in (".join(",",$allhave).")";
		//		$handel =$table->execute($sql);
		//		$temp = $handel->fetchAll();
		//
		//		$this->_view['manage'] =$temp;
		//dump($temp);
	}

	function actionWel(){
		
		$table =QDB::getConn();
		$sql = "SELECT DISTINCT groupid FROM `grouptohost`";
		$handel =$table->execute($sql);
		$temp = $handel->fetchAll();
		$allhave = array();
		foreach ($temp as $s){
			array_push ($allhave,$s['groupid']);
			//echo $s['groupid'];
		}
		$this->_view['allgroupstr']=join(",",$allhave);
		
		//dump($this->_view['manage']);
		//		$sql = "SELECT * FROM `group` where id in (".join(",",$allhave).")";
		//		$handel =$table->execute($sql);
		//		$temp = $handel->fetchAll();
		//
		//		$this->_view['manage'] =$temp;
		//dump($temp);
	}

	function actionState()
    {
        $this->_view['manage'] = Host::find('[group.groupid]= ?',$this->_context->groupid)->order('id desc')->getAll();
		
    }

	function actionStatelist()
    {
        $this->_view['manage'] = Host::find()->order('id desc')->getAll();
		
    }

	function actionBlind()
    {
        if ($this->_context->isPOST())
        {
			
			$a = array();
			$a =split('[,]', $_POST['hostid']);  
			//echo $_POST['hostid'];
			//print_r($a);
			foreach ($a as $key=>$value)
			{
				$newgroup= new Grouphost(array('hostid'=>$value,'groupid'=>$_POST['groupid']));
				$newgroup->save();
				//echo $value.'|';
			}
//			$form = new Form_HostgroupAdd(url('default::hostmanage/list'));
//			
//			try{
//				if($form->validate($_POST)){
//					$newgroup= new Hostgroup($form->values());
//					$newgroup->save();
//
//					//return '添加成功';
//				}
//			}catch(QValidation_FailedException $ex){
//				$form->invalidate($ex);
//			}
//
//			$this->_view['form']=$form;
		}
		
		//$this->_view['manage'] = Host::find('[group.groupid]= ?',$this->_context->groupid)->order('id desc')->getAll();
		
    }

	function actionAdminBlind()
    {
        if ($this->_context->isPOST())
        {
			$allout='';
			$a = array();
			$a =split('[,]', $_POST['userid']);  
			//echo $_POST['hostid'];
			//print_r($a);
			foreach ($a as $key=>$value)
			{
				// 查询指定用户 ID
				$edit_one = Groupuser::find('userid = ? and groupid = ?', $value,$_POST['groupid'])->query();
				if ( !$edit_one->id() )
				{ // 如果用户 ID 无效，则返回用户管理首页
					//return $this->_redirect(url('default::rulesetting/index'));
					$newgroup= new Groupuser(array('userid'=>$value,'groupid'=>$_POST['groupid']));
					$newgroup->save();
					$allout.= '添加成功！<br>';
				}else{
					$allout.= '此管理员已经是组管理员！<br>';
					//dump($edit_one->users->user_name);
				}
				
				
				//echo $value.'|';
			}
//			$form = new Form_HostgroupAdd(url('default::hostmanage/list'));
//			
//			try{
//				if($form->validate($_POST)){
//					$newgroup= new Hostgroup($form->values());
//					$newgroup->save();
//
//					//return '添加成功';
//				}
//			}catch(QValidation_FailedException $ex){
//				$form->invalidate($ex);
//			}
//
//			$this->_view['form']=$form;
		}
		echo $allout;
		//$this->_view['manage'] = Host::find('[group.groupid]= ?',$this->_context->groupid)->order('id desc')->getAll();
		
    }
}